# 搜索分页
from django.core.paginator import Paginator


def search_page_break(request, quset, search_field, pageNum, pageIndex=1):
    # 搜索条件
    searchKey = []
    # 获取并判断搜索条件
    search_key = request.GET.get("searchKey", None)  # 获取表单提交搜索的值
    if search_key:  # 如果 search_key 有值
        quset = quset.filter(**{search_field: search_key})  # 这种只能进行并且的查询，or查询不能应用
        # for o in obj:
        #     print(o.search_key)
        searchKey.append("searchKey=" + search_key)

    # 分页处理
    pageIndex = int(pageIndex)  # 第几页,当前页
    page = Paginator(quset, pageNum)  # 每页多少条
    maxPage = page.num_pages  # 获取最大页数
    # 处理页码越界
    if pageIndex > maxPage:
        pageIndex = maxPage
    if pageIndex < 1:
        pageIndex = 1
    quset = page.page(pageIndex)  # 获取当前页数据
    pages = page.page_range  # 获取页码列表信息
    page_info = {"QuerySet": quset, "pages": pages, "pageIndex": pageIndex, "maxPage": maxPage, "searchKey": searchKey}
    return page_info
